#include "fCanvasManager.h"
#include "fFramework.h"
#include "fCanvas.h"
#include "fBaseControl.h"
fCanvasManager::fCanvasManager( fEngine* aFramework )
{
    iFramework = aFramework;
}

fCanvasManager::~fCanvasManager( )
{
    
}

fControlBase* fCanvasManager::GetTopMostCtl( )
{
    if( iCurrentCanvas->iMenuBar && iCurrentCanvas->iMenuBar->CurrentState( ) != fMenu::KMenuHide )
    {
        return iCurrentCanvas->iMenuBar;
    }

    RPointerArray< fDialog >& topmosts = iCurrentCanvas->TopMost( );
    int count = topmosts.Count( );
    fControlBase* topTemp = NULL;
    for( int index = 0; index < count; index++ )
    {
        topTemp = topmosts[ index ];
        if( topTemp->IsVisible( ) )
        {
            return topTemp;
        }
    }
    return NULL;
}

fControlBase* fCanvasManager::GetCurrentCtl( )
{
    return iCurrentCanvas;
}

void fCanvasManager::ChangeCanvas( int aCanvasIndex, const TDesC& aCommandline, void* data )
{
    
    fCanvas* prevCanvas = iCurrentCanvas;
    if( prevCanvas )
    {
        prevCanvas->SwitchFromCanvas( );
        iFramework->HideCourse( );
    }

    DoChangeCanvas( aCanvasIndex, aCommandline, data );
    
    if( iCurrentCanvas )
    {
        if( prevCanvas != iCurrentCanvas )
        {
            iCurrentCanvas->PrevCanvas( prevCanvas );
        }
        
        iCurrentCanvas->SwitchToCanvas( aCommandline, data );
    }
    
}

void fCanvasManager::DoChangeCanvas( int /*aCanvasIndex*/, const TDesC& /*aCommandline*/, void* /*_data*/)
{
    _ASSERT_LOG( 0 );
}

void fCanvasManager::DeleteCanvas( fCanvas*/*canvas*/)
{
    _ASSERT_LOG( 0 );
}

void fCanvasManager::GoBackCanvas( )
{
    if( iCurrentCanvas )
    {
        if( iCurrentCanvas->PrevCanvas( ) )
        {
            iCurrentCanvas->SwitchFromCanvas( );
            iFramework->HideCourse( );
            fCanvas* prevViewCanvas = iCurrentCanvas->PrevCanvas( );
            DeleteCanvas( iCurrentCanvas );
            iCurrentCanvas = prevViewCanvas;
            iCurrentCanvas->SwitchBack( );
        }
        else
        {
            iCurrentCanvas->SwitchFromCanvas( );
            iFramework->HideCourse( );
            DoExit( );
        }
    }
    else
    {
        _ASSERT_LOG( 0 );
    }
}
